home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / shell / vector11.lha / src / vector.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-18  |  19.5 KB  |  840 lines

  1. /*
  2.     VECTOR FILE SYSTEM V1.1
  3.           _
  4.     by Guru Gnosis Sahib <gnosis@brahman.nullnet.fi>
  5.  
  6.     Written in SAS/C 6.5
  7.     Assumes 32-bit ints and no prototyping
  8.     Caveat emptor: Most of the code is old and yucky, and it shows.
  9.  
  10.     Version history:
  11.     V1.0  13-Jun-94  First fully functional version, limited release
  12.     V1.1  05-Nov-94  Validate improved; Quick Validate added; some bugs in
  13.                      ratio system fixed; considerable optimizing;
  14.                      de-kludging of some parts of the source
  15. */
  16.  
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <libraries/dos.h>
  22. #include <dos/dostags.h>
  23. #include <dos/var.h>
  24.  
  25. #define USER_MAX    250        /* Maximum users with Vector records */
  26. #define ULTYPE_MAX    10        /* Maximum upload categories */
  27. #define TYPE_MAX    20        /* Maximum file types */
  28. #define TYPE_UL        1
  29. #define TYPE_DL        2
  30. #define MOD_HARD    1        /* Immediate, single modification to data */
  31. #define MOD_SOFT    2        /* Modify data in memory only */
  32. #define ERROR        -1
  33.  
  34. #define LEAVE_DESC    1
  35. #define KILL_DESC    2
  36. #define DEF_RATIO    1
  37. #define MAN_RATIO    2
  38.  
  39. #define VERSION        "1.1"
  40.  
  41. struct UserData
  42. {
  43.     char User[20];
  44.     int DLByte;
  45.     int ULByte;
  46.     int Creds;
  47. };
  48.  
  49. struct UploadType
  50. {
  51.     char Name[40];
  52.     char Path[80];
  53.     int Level;
  54. };
  55.  
  56. struct FileType
  57. {
  58.     char Name[40];
  59.     char Path[80];
  60.     int Ratio;
  61. };
  62.  
  63. struct UserData ud[USER_MAX];
  64. struct UploadType ut[ULTYPE_MAX];
  65. struct FileType ft[TYPE_MAX];
  66.  
  67. typedef enum { L_VISITOR, L_GUEST, L_NORMAL, L_QUALIFIED, L_FRIEND,
  68.     L_TRUSTED, L_PRIVILEGED, L_ASSISTANT, L_SUPERUSER, L_WIZARD,
  69.     L_SYSTEM, L_DAEMON, L_UNKNOWN } lvl_id;
  70.  
  71. char *lvl_name[] = { "visitor", "guest", "normal", "qualified",    "friend",
  72.     "trusted", "privileged", "assistant", "superuser", "wizard",
  73.     "system", "daemon" };
  74.  
  75. int initbyte = 1000000;
  76.  
  77. /* The program */
  78.  
  79. main(int argc, char *argv[])
  80. {
  81.     FILE *input, *input2, *output;
  82.     BPTR lock;
  83.  
  84.     char vectorpath[80], descpath[80], ulpath[80], user[80], file[80],
  85.         newfile[80], lastuser[80], editor[80], temp[256], foo[80];
  86.     char *ptr, c;
  87.     struct tm *tp;
  88.      time_t t;
  89.     int ud_max = 1, ut_max = 0, ft_max = 0, def_ratio = 5, junk = 0;
  90.     int access, lastdate, lastbyte, date, type, flag, byte, i, j,
  91.         desc_type, ratio_type;
  92.  
  93.     if(!stricmp(argv[1], "?") || !stricmp(argv[1], "-h") ||
  94.         !stricmp(argv[1], "-?")) help();
  95.  
  96. /* Get env variables */
  97.  
  98.     GetVar("user", user, 80, GVF_LOCAL_ONLY);
  99.     GetVar("accesslevel", temp, 80, GVF_LOCAL_ONLY);
  100.     GetVar("visual", editor, 80, GVF_LOCAL_ONLY);
  101.     access = get_level(temp);
  102.  
  103. /* Initialize */
  104.     
  105.     strcpy(vectorpath, "AXsh:etc/vector/");
  106.     strcpy(descpath, "AXsh:etc/ffe/");
  107.     strcpy(ulpath, "Storage:Temp/");
  108.     if(!strcmp(editor, "")) strcpy(editor, "editor");
  109.  
  110. /* Parse rc.vector */
  111.  
  112.     if(!(input = fopen("AXsh:etc/rc.vector", "r")))
  113.         std_error("AXsh:etc/rc.wrap not found!");
  114.     while(!feof(input))
  115.     {
  116.         fgets(temp, 80, input);
  117.         if(temp[0] == '%')
  118.         {
  119.             if(!(strnicmp(temp, "%vectorpath:", 12)))
  120.             {
  121.                 fgets(vectorpath, 80, input);
  122.                 vectorpath[strlen(vectorpath) - 1] = 0;
  123.             }
  124.             if(!(strnicmp(temp, "%uploadpath:", 12)))
  125.             {
  126.                 fgets(ulpath, 80, input);
  127.                 ulpath[strlen(ulpath) - 1] = 0;
  128.             }
  129.             if(!(strnicmp(temp, "%initbyte:", 10)))
  130.             {
  131.                 fscanf(input, "%d", &initbyte);
  132.             }
  133.             if(!(strnicmp(temp, "%uploadtype:", 12)))
  134.             {
  135.                 flag = TRUE;
  136.                 while(flag == TRUE)
  137.                 {
  138.                     fgets(temp, 80, input);
  139.                     if(strlen(temp) < 3 || feof(input)) flag = FALSE;
  140.                     else
  141.                     {
  142.                         sscanf(temp, "%s %s %s", ut[ut_max].Name,
  143.                             ut[ut_max].Path, foo);
  144.                         i = get_level(foo);
  145.                         if(access >= i)
  146.                         {
  147.                             ut[ut_max].Level = i;
  148.                             ut_max++;
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.             if(!(strnicmp(temp, "%filetype:", 10)))
  154.             {
  155.                 flag = TRUE;
  156.                 while(flag == TRUE)
  157.                 {
  158.                     fgets(temp, 80, input);
  159.                     if(strlen(temp) < 3 || feof(input)) flag = FALSE;
  160.                     else
  161.                     {
  162.                         sscanf(temp, "%s %s %d", ft[ft_max].Name,
  163.                             ft[ft_max].Path, &ft[ft_max].Ratio);
  164.                         ft_max++;
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.     }
  170.     fclose(input);
  171.  
  172. /* Parse arguments: User commands */
  173.  
  174.     if(!stricmp(argv[1], "stat"))
  175.     {
  176.         if(argc > 2)
  177.         {
  178.             if(access < L_ASSISTANT)
  179.                 puts("Extra arguments ignored.");
  180.             else
  181.             {
  182.                 strcpy(user, argv[2]);
  183.                 printf("User %s - ", user);
  184.             }
  185.         }        
  186.  
  187.         byte = get_ratio(vectorpath, user);
  188.         if(byte == 0)
  189.         {
  190.             puts("No files uploaded or downloaded.");
  191.             exit(0);
  192.         }
  193.         printf("File credits: %d bytes (%d kilobytes)\n", byte, byte / 1024);
  194.         exit(0);
  195.     }
  196.  
  197.     if(!stricmp(argv[1], "check"))
  198.     {
  199.         byte = get_ratio(vectorpath, user);
  200.         if(byte >= 0) exit(0);
  201.         exit(5);
  202.     }
  203.  
  204.     if(!stricmp(argv[1], "dl"))
  205.     {
  206.         sprintf(temp, "%s.lastcheck", vectorpath);
  207.         if(!(input = fopen(temp, "r")))
  208.             std_error("Could not open .lastcheck file!");
  209.         fscanf(input, "%d\n%d\n%s\n", &lastdate, &lastbyte, lastuser);
  210.         fclose(input);
  211.  
  212.         ud_max = read_data(vectorpath);
  213.         if(ud_max == ERROR)
  214.             std_error("Could not open .ratio file!");
  215.         t = time(NULL);
  216.         tp = localtime(&t);
  217.  
  218.         if(!(input = fopen("AXsh:etc/adm/xpr.log", "r")))
  219.             std_error("Could not open xpr.log!");
  220.         fgets(temp, 80, input);        /* Header */
  221.         fgets(temp, 80, input);        /* Empty line */
  222.         fscanf(input, "%d %d %d %s %s", &date, &byte, &junk, user, temp);
  223.         fgets(temp, 80, input);        /* Rest of line */
  224.         flag = FALSE;
  225.         while(1)
  226.         {
  227.             if(date == lastdate && byte == lastbyte && !strcmp(user, lastuser))
  228.                 break;
  229.             fscanf(input, "%d %d %d %s %s", &date, &byte, &junk, user, temp);
  230.             fgets(temp, 80, input);        /* Rest of line */
  231.             if(feof(input))
  232.             {
  233.                 fclose(input);
  234.                 strftime(temp, sizeof(temp)-1, "%d%m%y %H%M: Corrupted xpr.log!  Regenerating .lastcheck.", tp);
  235.                 puts(temp);
  236.                 write_check(vectorpath, date, byte, user);
  237.                 exit(0);
  238.             }
  239.         }
  240.         flag = FALSE;
  241.         while(!feof(input))
  242.         {
  243.             fscanf(input, "%d %d %d %s %s", &date, &byte, &junk, user, temp);
  244.             if(!strcmp(temp, "Send"))        /* Download */
  245.             {
  246.                 flag = TRUE;
  247.                 fscanf(input, "%s %[^\n]\n", temp, file);
  248.                 ptr = strrchr(file, '/'); /* Chop off path in need */
  249.                 if(ptr != NULL)    strcpy(file, ptr + 1);
  250.                 strftime(temp, sizeof(temp)-1, "%H%M", tp);
  251.                 printf("%06d %s: DL '%s' (%d bytes) by %s\n", date, temp, file, byte, user);
  252.                 ud_max = mod_ratio(vectorpath, user, (long) -byte, ud_max, MOD_SOFT);
  253.                     /* Substract! */
  254.             }
  255.             if(!strcmp(temp, "Rcvd"))        /* Upload */
  256.             {
  257.                 flag = TRUE;
  258.                 fscanf(input, "%s %[^\n]\n", temp, file);
  259.                 ptr = strrchr(file, '/'); /* Chop off path in need */
  260.                 if(ptr != NULL)    strcpy(file, ptr + 1);
  261.                 strftime(temp, sizeof(temp)-1, "%H%M", tp);
  262.                 printf("%06d %s: UL '%s' (%d bytes) by %s\n", date, temp, file, byte, user);
  263.             }
  264.         }
  265.         if(flag == FALSE)
  266.         {
  267.             strftime(temp, sizeof(temp)-1, "%d%m%y %H%M: No new file transfers.", tp);
  268.             puts(temp);
  269.         }
  270.         fclose(input);
  271.         if(write_data(vectorpath, ud_max) == ERROR)
  272.             std_error("Could not modify .ratio file!");
  273.         write_check(vectorpath, date, byte, user);
  274.         exit(0);
  275.     }
  276.  
  277.     puts("\nVector File System v"VERSION" by Guru Gnosis Sahib");
  278.  
  279.     if(!stricmp(argv[1], "ul"))
  280.     {
  281.         if(System("List >T:Vector.tmp Storage:Temp QUICK NOHEAD", TAG_END) != 0)
  282.             std_error("Could not get file listing, aborting.");
  283.         if(!(input = fopen("T:Vector.tmp", "r")))
  284.             std_error("New file listing not found, aborting.");
  285.         fgets(file, 80, input);
  286.         sprintf(temp, "%s.newul", vectorpath);
  287.         if(!(output = fopen(temp, "a")))
  288.             std_error("Could not open .newul, aborting.");
  289.         while(!feof(input))
  290.         {
  291.             file[strlen(file) - 1] = 0;
  292.             printf("\nFile %s located.\n", file);
  293.             puts("Select a category:");
  294.             for(i = 0; i < ut_max; i++)
  295.             {
  296.                 printf("%d) %-20s\n", i + 1, ut[i].Name,
  297.                 ft[i].Ratio);
  298.             }
  299.             fgets(temp, 80, stdin);
  300.             type = (int) strtol(temp, foo, 10) - 1;
  301.             if(type >= ut_max || type < 0)
  302.             {
  303.                 puts("Illegal category, defaulting to 1.");
  304.                 type = 0;
  305.             }
  306.             sprintf(temp, "AddDesc \"%s\"", file);
  307.             if(System(temp, TAG_END) != 0)
  308.                 std_error("AddDesc utility not found, aborting.");
  309.             puts("Moving file...");
  310.             sprintf(temp, "%s%s", ulpath, file);
  311.             byte = get_size(temp);
  312.             sprintf(temp, "Move \"%s%s\" \"%s%s\"", ulpath, file, ut[type].Path, file);
  313.             if(System(temp, TAG_END) != 0)
  314.                 std_error("Error during file move, aborting.");
  315.             fprintf(output, "%s %d %s%s\n", user, byte, ut[type].Path, file);
  316.             fgets(file, 80, input);
  317.         }
  318.         fclose(input);
  319.         fclose(output);
  320.         remove("T:Vector.tmp");
  321.         puts("\nUploads scanned.\n");
  322.         exit(0);
  323.     }
  324.  
  325. /* Parse arguments: Superuser commands */
  326.  
  327.     if(access < L_ASSISTANT)
  328.     {
  329.         puts("Access denied.");
  330.         help();
  331.     }
  332.  
  333.     if(!stricmp(argv[1], "award"))
  334.     {
  335.         if(argc < 3) help();
  336.         strcpy(user, argv[2]);
  337.         byte = get_ratio(vectorpath, user);
  338.         if(byte == 0)
  339.             puts("No files uploaded or downloaded.");
  340.         else
  341.             printf("File credits: %d bytes (%d kilobytes)\n", byte, byte / 1024);
  342.         puts("Award how many credits?  Use a negative value to take away.");
  343.         scanf("%d", &junk);
  344.         ud_max = mod_ratio(vectorpath, user, junk, ud_max, MOD_HARD);
  345.         if(ud_max == ERROR)
  346.             std_error("Could not modify credits.");
  347.         printf("Done, user now has %d bytes of credits.\n\n", byte + junk);
  348.         exit(0);
  349.     }
  350.         
  351.     if(!stricmp(argv[1], "init"))
  352.     {
  353.         puts("This will erase all previous records!  Are you sure? (y/N)");
  354.         fgets(temp, 80, stdin);
  355.         if(strnicmp(temp, "y", 1)) std_error("Aborted.");
  356.         puts("Enter a default ratio as an integer (x bytes for each byte UL'd):");
  357.         scanf("%d", &def_ratio);
  358.         if(!(input = fopen("AXsh:etc/adm/xpr.log", "r")))
  359.             std_error("Could not open xpr.log!");
  360.         fgets(temp, 80, input);        /* Header */
  361.         fgets(temp, 80, input);        /* Empty line */
  362.         fscanf(input, "%d %d %d %s %s", &lastdate, &byte, &junk, user, temp);
  363.         if(!strcmp(temp, "Rcvd"))
  364.             type = TYPE_UL;
  365.         else
  366.             type = TYPE_DL;
  367.         fgets(temp, 80, input);        /* Rest of line */
  368.         while(!feof(input))
  369.         {
  370.             flag = -1;
  371.             for(i = 0; i < ud_max; i++)
  372.             {
  373.                 if(!strcmp(user, ud[i].User))
  374.                 {
  375.                     flag = i;
  376.                     break;
  377.                 }
  378.             }
  379.             if(flag == -1)
  380.             {
  381.                 strcpy(ud[ud_max].User, user);
  382.                 if(type == TYPE_DL)
  383.                 {
  384.                     ud[ud_max].DLByte = byte;
  385.                     ud[ud_max].ULByte = 0;
  386.                 }
  387.                 else
  388.                 {
  389.                     ud[ud_max].ULByte = byte;
  390.                     ud[ud_max].DLByte = 0;
  391.                 }
  392.                 ud_max++;
  393.             }
  394.             else
  395.             {
  396.                 if(type == TYPE_DL)
  397.                     ud[flag].DLByte += byte;
  398.                 else
  399.                     ud[flag].ULByte += byte;
  400.             }                
  401.             fscanf(input, "%d %d %d %s %s", &lastdate, &byte, &junk, user, temp);
  402.             if(!strcmp(temp, "Rcvd"))
  403.                 type = TYPE_UL;
  404.             else
  405.                 type = TYPE_DL;
  406.             fgets(temp, 80, input);        /* Rest of line */
  407.         }
  408.         fclose(input);
  409.         puts("\nVector xpr.log scan results:\n");
  410.         for(i = 1; i < ud_max; i++)
  411.         {
  412.             ud[i].Creds = ud[i].ULByte * def_ratio - ud[i].DLByte + initbyte;
  413.             printf("%-12s: %10d b UL %10d b DL %10d b creds\n",
  414.                 ud[i].User, ud[i].ULByte, ud[i].DLByte, ud[i].Creds);
  415.         }
  416.         puts("\nSaving ratios...");
  417.         if(write_data(vectorpath, ud_max) == ERROR)
  418.             std_error("Could not create .ratio file!");
  419.  
  420.         puts("Saving other data...");
  421.         if(write_check(vectorpath, lastdate, byte, user) == FALSE)
  422.             std_error("Could not update .lastcheck!");
  423.  
  424.         puts("Done!\n");
  425.         exit(0);
  426.     }
  427.  
  428.     if(!stricmp(argv[1], "new"))
  429.     {
  430.         sprintf(temp, "%s.newul", vectorpath);
  431.         if(!(input = fopen(temp, "r")))
  432.             std_error("No new uploads.\n");
  433.         puts("New uploads:");
  434.         fscanf(input, "%s %d %[^\n]", user, &byte, foo);
  435.         while(!feof(input))
  436.         {
  437.             printf("\nUploaded by %s: \"%s\", %d bytes\n", user, foo, byte);
  438.             puts("Description:");
  439.             ptr = strrchr(foo, '/'); /* Chop off path in need */
  440.             if(ptr != NULL)    strcpy(file, ptr + 1);
  441.             sprintf(temp, "%s%s.dsc", descpath, file);
  442.             if(!(input2 = fopen(temp, "r")))
  443.                 puts("None found");
  444.             else
  445.             {
  446.                 fgets(temp, 80, input2);
  447.                 while(!feof(input2))
  448.                 {
  449.                     if(strlen(temp) > 1) printf("%s", temp);
  450.                     fgets(temp, 80, input2);
  451.                 }
  452.                 fclose(input2);
  453.             }
  454.             fscanf(input, "%s %d %[^\n]", user, &byte, foo);
  455.         }
  456.         exit(0);
  457.     }
  458.  
  459.     if(!stricmp(argv[1], "scan"))
  460.     {
  461.         sprintf(temp, "%s.newul", vectorpath);
  462.         if(!(input = fopen(temp, "r")))
  463.             std_error("No new uploads.\n");
  464.         puts("Scanning for new uploads...");
  465.         fscanf(input, "%s %d %[^\n]", user, &byte, foo);
  466.         while(!feof(input))
  467.         {
  468.             ptr = strrchr(foo, '/'); /* Chop off path */
  469.             strcpy(file, ptr + 1);
  470.             printf("\nUploaded by %s: \"%s\", %d bytes\n", user, file, byte);
  471.             if((lock = Lock(foo, ACCESS_READ)) == 0)
  472.                 puts("File not found!  Skipping to next one...");
  473.             else
  474.             {
  475.                 UnLock(lock);
  476.                 puts("Description:");
  477.                 sprintf(temp, "%s%s.dsc", descpath, file);
  478.                 if(!(input2 = fopen(temp, "r")))
  479.                     puts("None found");
  480.                 else
  481.                 {
  482.                     fgets(temp, 80, input2);
  483.                     while(!feof(input2))
  484.                     {
  485.                         if(strlen(temp) > 1) printf("%s", temp);
  486.                         fgets(temp, 80, input2);
  487.                     }
  488.                     fclose(input2);
  489.                 }
  490.                 puts("(V)alidate, (Q)uick Validate, (D)elete, (I)gnore, or (A)bort?");
  491.                 fgets(temp, 80, stdin);
  492.                 switch(tolower(temp[0]))
  493.                 {
  494.                 case 'a':
  495.                     puts("Aborting, no changes made to new uploads list.");
  496.                     fclose(input);
  497.                     exit(0);
  498.  
  499.                 case 'i':
  500.                     puts("Ignored: no credits awarded, file not affected.");
  501.                     break;
  502.  
  503.                 case 'd':                
  504.                     puts("Deleting file and description...\n");
  505.                     if(remove(foo) != 0)
  506.                         std_warn("Could not delete file");
  507.                     sprintf(temp, "%s%s.dsc", descpath, file);
  508.                     if(remove(temp) != 0)
  509.                         std_warn("Could not delete description");
  510.                     break;
  511.  
  512.                 case 'v':
  513.                     puts("Modify description? (y/N)");
  514.                     fgets(temp, 80, stdin);
  515.                     if(!strnicmp(temp, "y", 1))
  516.                     {
  517.                         sprintf(temp, "%s \"%s%s.dsc\"", editor, descpath, file);
  518.                         if(System(temp, TAG_END) != 0)
  519.                             std_error("Could not edit description");
  520.                     }
  521.  
  522.                     puts("Rename file? (y/N)");
  523.                     fgets(temp, 80, stdin);
  524.                     if(!strnicmp(temp, "y", 1))
  525.                     {
  526.                         puts("Enter a new filename:");
  527.                         fgets(newfile, 80, stdin);
  528.                         newfile[strlen(newfile) - 1] = 0;
  529.                         sprintf(temp, "Move \"%s%s.dsc\" \"%s%s.dsc\"", descpath, file, descpath, newfile);
  530.                         if(System(temp, TAG_END) != 0)
  531.                             std_error("Could not rename file description");
  532.                         strcpy(file, newfile);
  533.                     }
  534.  
  535.                     /* FALL THRU */
  536.  
  537.                 case 'q':
  538.                     strcpy(newfile, file);
  539.                     puts("\n\
  540. A) Move to any directory, leave desc, default ratio\n\
  541. B) Move to any directory, leave desc, manual ratio\n\
  542. C) Move to any directory, delete desc, default ratio\n\
  543. D) Move to any directory, delete desc, manual ratio\
  544.                     ");
  545.  
  546.                     for(i = 0; i < ft_max; i++)
  547.                     {
  548.                     printf("%d) Type %-20s (ratio %2d:1)\n", i + 1, ft[i].Name,
  549.                             ft[i].Ratio);
  550.                     }
  551.                     puts("\nSelect a category:");
  552.                     fgets(temp, 80, stdin);
  553.                     c = toupper(temp[0]);
  554.                     switch(c)
  555.                     {
  556.                     case 'A':
  557.                         ratio_type = DEF_RATIO;
  558.                         desc_type = LEAVE_DESC;
  559.                         break;
  560.  
  561.                     case 'B':
  562.                         ratio_type = MAN_RATIO;
  563.                         desc_type = LEAVE_DESC;
  564.                         break;
  565.  
  566.                     case 'C':
  567.                         ratio_type = DEF_RATIO;
  568.                         desc_type = KILL_DESC;
  569.                         break;
  570.  
  571.                     case 'D':
  572.                         ratio_type = MAN_RATIO;
  573.                         desc_type = KILL_DESC;
  574.                         break;
  575.                     }
  576.                     j = (int) strtol(temp, lastuser, 10) - 1;
  577.                     i = j;
  578.                     if(i == -1)    /* ABCD */
  579.                     {
  580.                         i = TYPE_MAX - 1;
  581.                         puts("Enter full path for file: (include / or :)");
  582.                         fgets(temp, 80, stdin);
  583.                         temp[strlen(temp) - 1] = 0;
  584.                         strcpy(ft[i].Path, temp);
  585.                         if(ratio_type == MAN_RATIO)
  586.                         {
  587.                             puts("Enter ratio for file:");
  588.                             fgets(temp, 80, stdin);
  589.                             ft[i].Ratio = strtol(temp, temp, 10);
  590.                         }
  591.                         else ft[i].Ratio = def_ratio;
  592.  
  593.                         if(desc_type == KILL_DESC)
  594.                         {
  595.                             sprintf(temp, "%s%s.dsc", descpath, newfile);
  596.                             if(remove(temp) != 0)
  597.                                 std_warn("Could not delete description");
  598.                         }
  599.                     }
  600.                     puts("Adding credits...");
  601.                     sprintf(temp, "Move \"%s\" \"%s%s\"", foo, ft[i].Path, newfile);
  602.                     if(System(temp, TAG_END) != 0)
  603.                         std_error("Could not move file");
  604.                     ud_max = mod_ratio(vectorpath, user, byte * ft[i].Ratio, ud_max, MOD_HARD);
  605.                     if(ud_max == ERROR)
  606.                         std_error("Could not update ratio");
  607.                     printf("%d bytes awarded.\n", byte * ft[i].Ratio);
  608.                     break;
  609.  
  610.                 default:
  611.                     junk = -1;
  612.                 }
  613.             }
  614.             if(junk == 0)
  615.                 fscanf(input, "%s %d %[^\n]", user, &byte, foo);
  616.             else
  617.                 junk = 0;
  618.         }
  619.         fclose(input);
  620.         puts("Scan complete.");
  621.         sprintf(temp, "%s.newul", vectorpath);
  622.         remove(temp);
  623.         exit(0);
  624.     }
  625.     puts("Invalid arguments!");
  626.     help();
  627. }
  628.  
  629. /* Subprograms for access to ratio file, either in memory (MOD_SOFT) or
  630.    on disk (MOD_HARD) */
  631.  
  632. get_ratio(char path[80], char user[80])
  633. {
  634.     FILE *input;
  635.     char temp[80];
  636.     long creds = 0, flag = FALSE;
  637.  
  638.     sprintf(temp, "%s.ratio", path);
  639.     input = fopen(temp, "r");
  640.     if(input)
  641.     {
  642.         while(!feof(input))
  643.         {
  644.             fscanf(input, "%s %d", temp, &creds);
  645.             if(!stricmp(temp, user))
  646.             {
  647.                 flag = TRUE;
  648.                 break;
  649.             }
  650.         }
  651.         fclose(input);
  652.     }
  653.     if(flag == FALSE) return(0);
  654.     return(creds);
  655. }
  656.  
  657. make_ratio(char path[80], char user[80], long cred, int ud_max, int type)
  658. {
  659.     FILE *output;
  660.     char temp[80];
  661.  
  662.     if(type == MOD_HARD)
  663.     {
  664.         sprintf(temp, "%s.ratio", path);
  665.         if(!(output = fopen(temp, "a")))
  666.             return(FALSE);
  667.         fprintf(output, "%s %d\n", user, cred + initbyte);
  668.         fclose(output);
  669.     }
  670.     else
  671.     {
  672.         strcpy(ud[ud_max].User, user);
  673.         ud[ud_max].Creds = cred + initbyte;
  674.         ud_max++;
  675.     }
  676.     return(ud_max);
  677. }
  678.  
  679. mod_ratio(char path[80], char user[80], long newcred, int ud_max, int type)
  680. {
  681.     FILE *input, *output;
  682.     char temp[80];
  683.     long cred = 0, flag = FALSE, i;
  684.  
  685.     if(type == MOD_HARD)
  686.     {
  687.         sprintf(temp, "%s.ratio", path);
  688.         if(!(input = fopen(temp, "r")))
  689.         {
  690.             puts("Error: Could not open ratio data file");
  691.             return(ERROR);
  692.         }
  693.         if(!(output = fopen("PIPE:Vector.TMP", "w")))
  694.         {
  695.             fclose(input);
  696.             puts("Error: Could not open temporary file");
  697.             return(ERROR);
  698.         }        
  699.         while(!feof(input))
  700.         {
  701.             fscanf(input, "%s %d", temp, &cred);
  702.             if(!feof(input))
  703.             {
  704.                 if(!stricmp(temp, user))
  705.                 {
  706.                     fprintf(output, "%s %d\n", temp, cred + newcred);
  707.                     flag = TRUE;
  708.                 }
  709.                 else
  710.                     fprintf(output, "%s %d\n", temp, cred);
  711.             }
  712.         }
  713.         fclose(input);
  714.         fclose(output);
  715.         if(flag == FALSE)
  716.         {
  717.             ud_max = make_ratio(path, user, newcred, ud_max, MOD_HARD);
  718.             if(ud_max == FALSE)
  719.                 std_error("Could not create new record");
  720.         }
  721.         else
  722.         {
  723.             sprintf(temp, "Copy PIPE:Vector.TMP %s.ratio", path);
  724.             System(temp, TAG_END);
  725.         }
  726.     }
  727.     else
  728.     {
  729.         flag = ERROR;
  730.         for(i = 0; i < ud_max; i++)
  731.         {
  732.             if(!strcmp(user, ud[i].User))
  733.             {
  734.                 flag = i;
  735.                 break;
  736.             }
  737.         }
  738.         if(flag == ERROR)
  739.         {
  740.             ud_max = make_ratio(path, user, newcred, ud_max, MOD_SOFT);
  741.             if(ud_max == FALSE)
  742.                 std_error("Could not create new user record");
  743.         }
  744.         else
  745.             ud[flag].Creds += newcred;
  746.     }
  747.     return(ud_max);
  748. }
  749.  
  750. read_data(char path[80])
  751. {
  752.     FILE *input;
  753.     char temp[80];
  754.     int ud_max = 0;
  755.  
  756.     sprintf(temp, "%s.ratio", path);
  757.     if(!(input = fopen(temp, "r"))) return(ERROR);
  758.     while(!feof(input))
  759.     {
  760.         fscanf(input, "%s %d\n", ud[ud_max].User, &ud[ud_max].Creds);
  761.         ud_max++;
  762.         if(ud_max == USER_MAX)
  763.         {
  764.             fclose(input);
  765.             std_error("Too many users with records, consult documentation.");
  766.         }    
  767.     }
  768.     fclose(input);
  769.     return(ud_max);
  770. }
  771.  
  772. write_data(char path[80], int ud_max)
  773. {
  774.     FILE *input;
  775.     char temp[80];
  776.     int i;
  777.  
  778.     sprintf(temp, "%s.ratio", path);
  779.     if(!(input = fopen(temp, "w")))    return(ERROR);
  780.     for(i = 0; i < ud_max; i++)
  781.     {
  782.         fprintf(input, "%s %d\n", ud[i].User, ud[i].Creds);
  783.     }
  784.     return(TRUE);
  785. }
  786.  
  787. /* Other subprograms */
  788.  
  789. write_check(char path[80], int date, int byte, char user[80])
  790. {
  791.     FILE *output;
  792.     char temp[80];
  793.  
  794.     sprintf(temp, "%s.lastcheck", path);
  795.     if(!(output = fopen(temp, "w"))) return(ERROR);
  796.     fprintf(output, "%d\n%d\n%s\n", date, byte, user);
  797.     fclose(output);
  798.     return(TRUE);
  799. }
  800.  
  801. get_size(char file[80])
  802. {
  803.     int size, fd;
  804.     
  805.     fd = open(file, O_RDONLY);
  806.     size = lseek(fd, 0L, 2);
  807.     close(fd);
  808.     return(size);
  809. }
  810.  
  811. get_level(char *temp)
  812. {
  813.     int lvl;
  814.  
  815.     for(lvl = 0; lvl < L_UNKNOWN; lvl++)
  816.     {
  817.         if(!stricmp(temp, lvl_name[lvl])) break;
  818.     }
  819.     return(lvl);
  820. }
  821.  
  822. help()
  823. {
  824.     puts("Syntax: (normal)    Vector [check | dl | stat | ul]");
  825.     puts("        (superuser) Vector [award | init | new | scan]");
  826.     puts("Consult documentation for more info.\n");
  827.     exit(0);
  828. }
  829.  
  830. std_warn(char *temp)
  831. {
  832.     printf("Warning: %s\n", temp);
  833. }
  834.  
  835. std_error(char *temp)
  836. {
  837.     printf("Warning: %s\n", temp);
  838.     exit(5);
  839. }
  840.